// Simple Test of Node Switch. right now only one switch can be attached, uses Interrupt 1
// 12-04-2010 <mdoll@usf.edu> http://opensource.org/licenses/mit-license.php

#include <Ports.h>
#include <RF12.h>
#include <HomeNet.h>
#include <HomeNetDevices.h>

//Start HomeNet packet stack
HomeNet stack(0x05);//0x01 is RF12 base station //0xFF is PC uplink

//Setup network adapters
HomeNetPortRF12   portRF12(stack);

//Setup attached devices
HomeNetDeviceJeeNode jeeNode(stack);
HomeNetDeviceStatusLights statusLights(stack);
HomeNetDeviceSwitch lswitch(stack);

//Package the setup info in a nice neat arrays
HomeNetPort * ports[] = {&portRF12};//,&portSerial,
HomeNetDevice * devices[] = {&jeeNode, &statusLights, &lswitch}; 
 
//delay (sec), frequency (sec), device, sendToNode, sendToDevice, Command, Payload
HomeNetInterrupt interrupt[] = {{&lswitch,7,2,CMD_GETVALUE,0}};

//Setup interrupt
volatile uint8_t deviceInterrupt = 0;

void setup() {
  //Initialize HomeNet with the setup info
  stack.init(ports, sizeof(ports)/2, devices, sizeof(devices)/2); 
  stack.registerStatusLights(statusLights); //setup status lights
  stack.registerInterrupts(interrupt,sizeof(interrupt)/sizeof(interrupt[0]));

  attachInterrupt(1, onInterrupt, CHANGE); //Setup interrupt
}

//Setup interrupt
void onInterrupt(){
  deviceInterrupt++;
}

void loop() {
  //Receive incoming packets
  stack.receive();
  stack.process();

  stack.deviceUpdate();
  
  //Still working on how often to call these
  stack.receive();
  stack.process();
  
  //setup interrupt
  if(deviceInterrupt > 0){
    deviceInterrupt = 0;
    stack.deviceInterrupt();  
  }
}


